package com.softer.chengxin.scheduled;

import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import com.softer.chengxin.score.entity.ScoreRecord;
import com.softer.chengxin.score.service.impl.SDManagerServiceImpl;
import com.softer.chengxin.score.util.ScoreUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/1/18
 */
public class TaskRunnable implements Runnable {
    @Autowired
    private ScheduledTaskDao scheduledTaskDao;
    @Autowired
    private ScoreUtil scoreUtil;

    @Override
    public void run() {
        //任务逻辑代码部分.
        //获取年度
        String sysYear = DateFormatUtil.getSysYear();
        //获取上一年
        String lastYear = DateFormatUtil.getLastYear();
        List<ScoreRecord> scoreRecordList = scheduledTaskDao.getScoreRecordList(lastYear);
        for (ScoreRecord scoreRecord : scoreRecordList) {
            //计算总得分
            BigDecimal allScore = scoreUtil.countScore(scoreRecord);
            String level = "-";
            if (allScore.compareTo(new BigDecimal(0)) > 0) {
                //计算等级
                level = scoreUtil.getLevel(allScore);
            }
            scoreRecord.setDj(level);
            scoreRecord.setAllScore(allScore);
            //更新上一年信用等级
            scheduledTaskDao.updateScoreRecord(scoreRecord);
            //新增当年信用等级
            scoreRecord.setYear(sysYear);
            scheduledTaskDao.addScoreRecord(scoreRecord);
        }
    }
}
